<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login-form"
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
        >
          <h1>Hello</h1>
          <h2>欢迎来到尚硅谷</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              :prefix-icon="Lock"
              placeholder="请输入密码"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login-btn"
              :loading="loading"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { User, Lock } from '@element-plus/icons-vue';
import { ElNotification } from 'element-plus';
import useUserStore from '@/store/modules/user';
import { getTime } from '@/utils/time';
import { tr } from 'element-plus/es/locale/index.mjs';

const useStore = useUserStore();
//获取路由器
const $router = useRouter();
//路由对象
const $route = useRoute();

const loginForm = reactive({
  username: '',
  password: '',
});
let loading = ref(false);
const loginFormRef = ref();

const validateUsername = (rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback();
  } else {
    callback(new Error('账号长度至少五位'));
  }
};

const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback();
  } else {
    callback(new Error('密码长度至少六位'));
  }
};

const rules = reactive({
  username: [{ validator: validateUsername, trigger: 'change' }],
  password: [{ validator: validatePassword, trigger: 'change' }],
});

const login = async () => {
  await loginFormRef.value.validate();
  loading.value = true;
  try {
    await useStore.userLogin(loginForm);
    $router.push('/');
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好`,
    });
    loading.value = false;
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    });
    loading.value = false;
  }
};
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
  }
  h1 {
    color: #fff;
    font-size: 40px;
  }
  h2 {
    padding: 20px 0;
    color: #fff;
    font-size: 20px;
  }
  .login-btn {
    width: 100%;
  }
}
</style>
